package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Categories;
import cn.springboot.erp.entity.Products;
import cn.springboot.erp.entity.Warehouses;
import cn.springboot.erp.mapper.CategoriesMapper;
import cn.springboot.erp.mapper.ProductsMapper;
import cn.springboot.erp.mapper.WarehousesMapper;
import cn.springboot.erp.vo.InventoryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.Inventory;
import cn.springboot.erp.service.InventoryService;
import cn.springboot.erp.mapper.InventoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【inventory】的数据库操作Service实现
* @createDate 2024-05-11 11:56:02
*/
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory>
    implements InventoryService{

    @Resource
    private WarehousesMapper warehouseMapper;
    @Resource
    private ProductsMapper productMapper;
    @Resource
    private CategoriesMapper categoriesMapper;
    @Override
    public ResponseResult listPage(QueryParams<Inventory> queryParams) {
        // 获取分页数据
        Integer limit = queryParams.getLimit();
        Integer page = queryParams.getPage();
        Inventory searchParams = queryParams.getSearchParams();
        // 分页
        Page<Inventory> inventoryPage = new Page<>(page, limit);
        LambdaQueryWrapper<Inventory> lambda = new QueryWrapper<Inventory>().lambda();
        lambda.eq(ObjectUtils.isNotNull(searchParams.getProductId()),Inventory::getProductId,searchParams.getProductId())
                .eq(ObjectUtils.isNotNull(searchParams.getWarehouseId()),Inventory::getWarehouseId,searchParams.getWarehouseId())
                .orderByDesc(Inventory::getCreateTime);
        baseMapper.selectPage(inventoryPage,lambda);

        // 遍历数据列表
        List<Inventory> inventories = inventoryPage.getRecords();
        System.out.println("inventories = " + inventories);
        List<InventoryVO> inventoryList = inventoryPage.getRecords().stream().map(inventory -> {
            InventoryVO inventoryVO = new InventoryVO();
            BeanUtils.copyProperties(inventory, inventoryVO);

            // 获取仓库id
            Integer warehouseId = inventory.getWarehouseId();
            // 根据仓库id查询仓库信息
            Warehouses warehouse = warehouseMapper.selectById(warehouseId);
            inventoryVO.setWarehouses(warehouse);

            // 获取商品id
            Integer productId = inventory.getProductId();
            Products product = productMapper.selectById(productId);
            inventoryVO.setProducts(product);
            // 获取商品类型id
            Integer categoryId = product.getCategoryId();
            // 查询商品类型表
            Categories categories = categoriesMapper.selectById(categoryId);
            inventoryVO.setCategoryName(categories.getName());
            return inventoryVO;
        }).collect(Collectors.toList());
        return ResponseResult.Success()
                .data("total",inventoryPage.getTotal())
                .data("inventoryList",inventoryList);
    }
}




